//
//  WYFWebInterface.m
//  NetEaseFinancial
//
//  Created by 吴玉峰 on 8/17/15.
//  Copyright (c) 2015 吴玉峰. All rights reserved.
//
#import "WYFWebInterface.h"

#import "WYFJSCommand.h"
#import "WYFJSBridge.h"

@implementation WYFWebInterface

- (NSString *)execute:(NSString *)cmdString {
    NSLog(@"interface.execute.cmdString = %@", cmdString);

    NSData *data = [cmdString dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error;
    NSDictionary *cmdObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

    WYFJSCommand *cmd = [[WYFJSCommand alloc] initCommandWithJsonObj:cmdObj jsBridge:super.jsBridge];
    NSLog(@"interface.execute.cmd.description = %@", cmd.description);

    if ([cmd.type intValue] == EXECUTE_NATIVE_FUNCTION_SYNC) {
        [self.jsBridge executeObjectiveCFunction:cmd];
        NSLog(@"interface.execute.cmdResult = %@", cmd.description);
        return cmd.description;
    } else {
        [self.jsBridge handleCommand:cmd];
        return nil;
    }
    return nil;
}

- (void)setResult:(NSString *)cmdString {
    NSLog(@"interface.setResult.cmdString = %@", cmdString);

    NSData *data = [cmdString dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error;
    NSDictionary *cmdObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

    WYFJSCommand *cmd = [[WYFJSCommand alloc] initCommandWithJsonObj:cmdObj jsBridge:super.jsBridge];

    cmd.type = [NSNumber numberWithInt:EXECUTE_JAVASCRIPT_CALLBACK];
    [self.jsBridge handleCommand:cmd];
}

@end
